// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});

google.setOnLoadCallback(function (){
	//angular.bootstrap(document.body, ['MicroCampusApplication']);
});
adminPanel.controller("UserStudentSummaryController",['cacheFactory','$http','$scope','$q','$filter',function(cacheFactory,$http,$scope,$q,$filter){
	this.msg="ok";
	$scope.showGrid=false;
	$scope.overallGrid={};
	$scope.selectedStudent="";
	var  thisObject=this;
	//use to send data to server
	$scope.searchCriteria={studentEntityId:0,pageNumber:1,limit:3};
	$scope.studentProfiles=null;
	$scope.selectedStudentProfile=null;
	
	//advance filter variables
	$scope.totalSubjectLoop=[];
	$scope.totalYearLoop=[];
	$scope.subjectSelection=[];
	$scope.yearSelection=[];
	$scope.termSelection=[];
	$scope.periodSelection=[];
	 //1 for bar chart and 2 for line chart
	$scope.chartTypes = [
	                 {name:'Bar Chart', value:1},
	                 {name:'Line Chart', value:2}
	               ];
	$scope.chartType=$scope.chartTypes[0];
	
	$scope.periods = [
		                 {name:'Year Wise', value:1},
		                 {name:'Term Wise', value:2}
		               ];
	$scope.period=$scope.periods[0];
	$scope.searchCriteria.periodSelection=$scope.period.value;
	this.mainContentUrl="";
	this.setPage=function(url){
		this.mainContentUrl=url;
		thisObject.performCallBackBasedOnUrl();
	};
	
	this.getDetails= function(){                 
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_OVERALL_GRID",$scope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				$scope.overallGrid=data.result[0];	
				$scope.showGrid=true;
			}else{
				alert(data.status +":"+data.msg);
				$scope.showGrid=true;
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
		
	};
	
	this.selectStudent=function(id){
		var  selected = $filter('filter')($scope.studentProfiles, {entityId: id});
		$scope.searchCriteria.studentEntityId=id;
		$scope.selectedStudent=selected[0].firstName +" "+ selected[0].lastName;
		$scope.selectedStudentProfile=selected[0];
		//render only on first page or overall student dashboard
		thisObject.performCallBackBasedOnUrl();
		
		//reset adavance filter variables
		$scope.totalSubjectLoop=[];
		$scope.totalYearLoop=[];
		$scope.subjectSelection=[];
		$scope.yearSelection=[];
		$scope.termSelection=[];
		$scope.periodSelection=[];
		$scope.searchCriteria.subjectSelection=[];
		$scope.searchCriteria.yearSelection=[];
		$scope.searchCriteria.termSelection=[];
	};
	
	this.initUserDashboard=function(userId){
		thisObject.getStudentInformation(userId);
		
	};
	
	//render all four quads on the page
	this.renderGraphs=function(){
		thisObject.getDetails();
		thisObject.getGraphData();
	};
	
	//get information about child linked with parent user id
	this.getStudentInformation=function(userId){
		var defer = $q.defer();
		defer.promise.then(function () {
			thisObject.renderGraphs();
		});
		
		$scope.searchCriteria.userId=userId;
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_GET_STUDENT_DATA",$scope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				$scope.studentProfiles=data.result;
				$scope.searchCriteria.studentEntityId=$scope.studentProfiles[0].entityId;
				$scope.selectedStudent=$scope.studentProfiles[0].firstName +" " + $scope.studentProfiles[0].lastName;
				$scope.selectedStudentProfile=$scope.studentProfiles[0];
				defer.resolve();
			}else{
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};

	
	this.getGraphData= function(){
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_OVERALL_GRAPH",$scope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				
				//var data = google.visualization.arrayToDataTable(data.result[0].objects);
				var json = data.object;
				var data1= new google.visualization.DataTable(json);

				var options = {
						title: 'Overall Performance',
						hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
				};
				// Instantiate and draw our chart, passing in some options.
				var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
				chart.draw(data1, options);
				//$scope.showGrid=true;
			}else{
				//alert(data.status +":"+data.msg);
				//$scope.showGrid=true;
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	
	
	this.getAdvancedGraphData= function(){
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_ADVANCED_GRAPH",$scope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				//var data = google.visualization.arrayToDataTable(data.result[0].objects);
				var json = data.object;
				var data1= new google.visualization.DataTable(json);
				var options = {
						title: 'Overall Performance',
						hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
				};
				var chart=null;
				// Instantiate and draw our chart, passing in some options.
				if($scope.chartType.value=="1"){
					chart = new google.visualization.ColumnChart(document.getElementById('advance_chart_div'));
					chart.draw(data1, options);
				}
				
				if($scope.chartType.value=="2"){
					chart = new google.visualization.LineChart(document.getElementById('advance_chart_div'));
					chart.draw(data1, options);
				}
				
				//$scope.showGrid=true;
			}else{
				//alert(data.status +":"+data.msg);
				//$scope.showGrid=true;
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	
	this.performCallBackBasedOnUrl=function(){
		if(thisObject.mainContentUrl.indexOf("user_dashboard")>-1){
			thisObject.renderGraphs();
		}
		if(thisObject.mainContentUrl.indexOf("student_advanced_graph")>-1){
			thisObject.getAdvancedGraphData();
		}
	};
	
	this.showFilters=function(){
		$scope.totalSubjectLoop=[];
		for (var i=0; i<(($scope.selectedStudentProfile.subjectList.length/4)+1); i++){
			$scope.totalSubjectLoop.push(i);
		}
		$scope.totalYearLoop=[];
		for (var i=0; i<(($scope.selectedStudentProfile.totalGradeInfo.length/4)+1); i++){
			$scope.totalYearLoop.push(i);
		}
		thisObject.toggleFilters();
	};
	
	this.toggleFilters=function(){
		$('#filterModal').modal('toggle');
	};
	
	this.applyFliters=function(chartType,period){
		$scope.chartType=chartType;
		$scope.period=period;
		$scope.searchCriteria.periodSelection=period.value;
		thisObject.getAdvancedGraphData();
		thisObject.toggleFilters();
	};
	
	this.toggleSubjectSelection = function(subjectId) {
		var idx = $scope.subjectSelection.indexOf(subjectId);
		// is currently selected
		if (idx > -1) {
			$scope.subjectSelection.splice(idx, 1);
		}
		// is newly selected
		else {
			$scope.subjectSelection.push(subjectId);
		}
		
		$scope.searchCriteria.subjectSelection=$scope.subjectSelection;
	};
	
	this.toggleYearSelection = function(year) {
		var idx = $scope.yearSelection.indexOf(year);
		// is currently selected
		if (idx > -1) {
			$scope.yearSelection.splice(idx, 1);
		}
		// is newly selected
		else {
			$scope.yearSelection.push(year);
		}
		$scope.searchCriteria.yearSelection=$scope.yearSelection;
	};
	
	this.toggleTermSelection = function(termId) {
		var idx = $scope.termSelection.indexOf(termId);
		// is currently selected
		if (idx > -1) {
			$scope.termSelection.splice(idx, 1);
		}
		// is newly selected
		else {
			$scope.termSelection.push(termId);
		}
		$scope.searchCriteria.termSelection=$scope.termSelection;
	};
	
}]);
